package com.regis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.regis.comment.CodeMsg;
import com.regis.comment.GlobalException;
import com.regis.entity.db.Dish;
import com.regis.entity.db.DishFlavor;
import com.regis.entity.db.Setmeal;
import com.regis.entity.db.SetmealDish;
import com.regis.entity.dto.DishDto;
import com.regis.service.DishFlavorService;
import com.regis.service.DishService;
import com.regis.mapper.DishMapper;
import com.regis.service.SetmealDishService;
import com.regis.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 纱雾休比酱
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2022-07-28 03:58:08
*/
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional//事务控制
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品和口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品表
        this.updateById(dishDto);
        //更新口味表
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        dishFlavorService.remove(queryWrapper.eq(DishFlavor::getDishId,dishDto.getId()));

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id删除菜品和对应的口味
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(Long... ids) {
        List<Long> dishIds = Arrays.asList(ids);
        //判断菜品是否与套餐有关联
        List<SetmealDish> setmealDishes =
                setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getDishId, dishIds));
        if(setmealDishes.size() > 0){
            HashSet<String> names = new HashSet<>();
            for(SetmealDish s : setmealDishes){
                names.add(s.getName());
            }
            throw new GlobalException(new CodeMsg("菜品：" + names + "与套餐有关联，删除失败" ));
        }

        //删除菜品
        this.removeBatchByIds(dishIds);
        //删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,dishIds);
        dishFlavorService.remove(queryWrapper);
    }
}




